//
//  FirstViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "FirstViewController.h"
#import "ReceiverListViewController.h"
#define image_tag 10000

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.leftBtn.hidden =YES;
    
    [self initBannerViews];     //轮播图
    [self initTableViews];
    
    headView = [MJRefreshHeaderView header];
    headView.delegate = self;
    headView.scrollView = _myTableView;

}

#pragma mark 创建轮播图UI
-(void)initBannerViews
{
    if (_pageScrollView == nil){
        self.pageScrollView = [[PageScrollView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH,124) andPageControllRect:CGRectMake(551/2.0f,112,76/2.0f,16/2.0f)];
    }
}

#pragma mark 创建底层TableView
-(void)initTableViews
{
    if(_myTableView == nil){
        CGRect rect = self.contentView.bounds;
        rect.size.height -=UI_TAB_BAR_HEIGHT;
        self.myTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.delegate =self;
        _myTableView.dataSource = self;
        [self.contentView addSubview:_myTableView];
        
        _myTableView.tableHeaderView = _pageScrollView;
        _myTableView.tableFooterView = [UIView initViewRect:CGRectMake(0, 0, UI_SCREEN_WIDTH, 10) andBackGroudColor:[UIColor clearColor]];
    }
}

#pragma mark UITableView Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 10+44+140+1.5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    ActivityCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil){
        cell = [[ActivityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.delegate = self;
    cell.dataDict = nil;
    return cell;
}

#pragma mark MJrefresh Delegate
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    [self performSelector:@selector(endHeaderRefreshing) withObject:nil afterDelay:1];
}
-(void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView
{

}

-(void)endHeaderRefreshing
{
    [headView endRefreshing];
}

#pragma mark ActivityCell Delegate
-(void)chooseGoods:(UITapGestureRecognizer *)tap
{
    UIView * goods = tap.view;
    NSLog(@"goods.tag = %d",goods.tag);
    ReceiverListViewController * receiverVC = [[ReceiverListViewController alloc] init];
    [self.navigationController pushViewController:receiverVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
